package fr.iut2.sil1.trolledit.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;

public class UploadFile  {
	
	private VerticalPanel panel = new VerticalPanel();
	private FormPanel uploadForm = new FormPanel();
	private FileUpload upload = new FileUpload();
	private HTML messageServeur = new HTML();
	Button uploadSubmitButton = new Button("Envoyer sur le serveur");
	private DialogBox dialogBox = new DialogBox();
	private static UploadFile SINGLETONUPLOAD;
	
	public UploadFile(){
		
		// Associe un service au formulaire
		uploadForm.setAction(GWT.getModuleBaseURL() + "uploadfile");
		
		// Pour ajouter un FileUpload widget, nous devons ajouter au formulaire
		// une methode d'envoie POST, et un encodage multipart MIME.
		uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
		uploadForm.setMethod(FormPanel.METHOD_POST);
		
		// Ajout du panel au formulaire
		uploadForm.setWidget(panel);
		
		// Proprietes du FileUpload widget.
		upload.setName("uploadFormElement");
		panel.add(upload);
		
		// Ajout du label message serveur
		panel.add(messageServeur);
		
		// Ajout d'un bouton 'Submit'
		
		panel.add(uploadSubmitButton);
		
		uploadSubmitButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				uploadForm.submit();
			}
		});
		
		uploadForm.addSubmitCompleteHandler(new SubmitCompleteHandler() {

			public void onSubmitComplete(SubmitCompleteEvent event) {
				messageServeur.setHTML(event.getResults());
				Window.alert("Upload complete  "+event.getResults());
			}
		});
		Button closeButton = new Button("Fermer");
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event){
				dialogBox.hide();
			}
		});
		panel.add(closeButton);
		dialogBox.setText("Telecharger une image pour votre troll");
		dialogBox.setWidget(panel);
		SINGLETONUPLOAD = this;
		
		//
	}
	
	public VerticalPanel getPanel() {
		return panel;
	}
	public void setPanel(VerticalPanel panel) {
		this.panel = panel;
	}
	public FormPanel getUploadForm() {
		return uploadForm;
	}
	public void setUploadForm(FormPanel uploadForm) {
		this.uploadForm = uploadForm;
	}
	public FileUpload getUpload() {
		return upload;
	}
	public void setUpload(FileUpload upload) {
		this.upload = upload;
	}
	public HTML getMessageServeur() {
		return messageServeur;
	}
	public void setMessageServeur(HTML messageServeur) {
		this.messageServeur = messageServeur;
	}

	public static UploadFile getSINGLETONUPLOAD() {
		return SINGLETONUPLOAD;
	}

	public static void setSINGLETONUPLOAD(UploadFile sINGLETONUPLOAD) {
		SINGLETONUPLOAD = sINGLETONUPLOAD;
	}

	public Button getUploadSubmitButton() {
		return uploadSubmitButton;
	}

	public void setUploadSubmitButton(Button uploadSubmitButton) {
		this.uploadSubmitButton = uploadSubmitButton;
	}

	public DialogBox getDialogBox() {
		return dialogBox;
	}

	public void setDialogBox(DialogBox dialogBox) {
		this.dialogBox = dialogBox;
	}

}
